package Clients;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.ImageIcon;

class Picture extends Canvas
{
  private static final long serialVersionUID = 1;
  private int   width      = 260;
  private int   height     = 260;
  private Image picture = null;
  private Point defLoc;

  public Picture()
  {
    setSize( width, height );
    defLoc = getLocation();
  }
  
  public Picture(int aWidth, int aHeight)
  {
    width = aWidth;
    height= aHeight;
    setSize( width, height );
    defLoc = getLocation();
  }
  
  public void setDefLoc(Point point){
	  defLoc = point;
  }

  public void set( ImageIcon ic )
  {
    picture = ic.getImage();         // Image to be drawn
    setBounds((int)defLoc.getX() + (int)this.getWidth()/2 - picture.getWidth(null)/2, (int)defLoc.getY() + (int)this.getHeight()/2 - picture.getHeight(null)/2, (int)getSize().getWidth(), (int)getSize().getHeight());//Centers the image
    repaint();
  }
    
  public void clear()
  {
	  picture = null;                  // clear picture
	  repaint();                          // Force repaint
  }

  public void paint( Graphics g )       // When 'Window' is first
  {                                     //  shown or damaged
    drawImage( (Graphics2D) g );
  }
  
  public void update( Graphics g )      // Called by repaint
  {                                     //
    drawImage( (Graphics2D) g );        // Draw picture
  }

  /**
    * Draw the picture
    * First set the area to white and then 
    *  draw the image 
    */

  public void drawImage( Graphics2D g )
  {
    setSize( width, height );
    g.setPaint( Color.BLACK );
    g.fill( new Rectangle2D.Double( 0, 0, width, height ) );
    if ( picture != null )
    {
      g.drawImage(picture, 0, 0, null);
    }
  }
}
